package com.lisynet.tools.mytools;

import cn.hutool.core.util.IdUtil;
import com.lisynet.tools.mytools.service.CacheService;
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;
import org.springframework.boot.SpringApplication;
import org.springframework.context.ConfigurableApplicationContext;

import java.util.concurrent.TimeUnit;

/**
 * @author lisy
 * @date 2022/1/23 8:17
 */
@BenchmarkMode(Mode.Throughput)
@Warmup(iterations = 3)
@Measurement(iterations = 10, time = 5)
@Threads(80)
@Fork(1)
@OutputTimeUnit(TimeUnit.SECONDS)
@State(Scope.Benchmark)
public class Benchmark {
    private ConfigurableApplicationContext context;
    private CacheService cacheService;

    @Setup
    public void init() {
        // 这里的WebApplication.class是项目里的spring boot启动类
        context = SpringApplication.run(MyToolsApplication.class);
        // 获取需要测试的bean
        this.cacheService = context.getBean(CacheService.class);

    }

    @org.openjdk.jmh.annotations.Benchmark
    public void forIndexIterate() {
        cacheService.set(IdUtil.simpleUUID(),IdUtil.simpleUUID());
    }

    public static void main(String[] args) throws Exception {
        Options options = new OptionsBuilder()
                .include(Benchmark.class.getSimpleName())
                .output("d:/Benchmark.log")
                .build();
        new Runner(options).run();
    }

    @TearDown
    public void down() {
        context.close();
    }
}
